/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ggsoft.webapp.action;

import com.ggsoft.timetracker.domain.Timecard;
import com.ggsoft.timetracker.service.TimeTrackerService;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.appfuse.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 *
 * @author giovanella
 * @date Jul 1, 2014
 */
public class TimecardForm extends BasePage implements Serializable {

    private static final long serialVersionUID = -1141119853856863204L;
    private Long id;
    private TimeTrackerService timeTrackerService;
    private Timecard timecard;
    private Date loginTime;
    private Date logoutTime;
    private User user;
    private List<User> users;
    private String operation;
    private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("h:mm a");

    public TimecardForm() {
    }

    @Autowired
    public void setTimeTrackerService(@Qualifier("timeTrackerService") TimeTrackerService timeTrackerService) {
        this.timeTrackerService = timeTrackerService;
    }

    public String add() {
        operation = "add";
        timecard = new Timecard();

        return "add";
    }

    public String cancel() {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'cancel' method");
        }

        return "list";
    }

    public String edit() {
        operation = "edit";
        System.out.println("Operation " + operation);
        String param = getParameter("id");
        id = Long.valueOf(param);
        timecard = timeTrackerService.findTimecard(id);
        loginTime = timecard.getLoginTime();
        logoutTime = timecard.getLogoutTime();

        return "edit";
    }

    public String save() {
        if (loginTime != null) {
            Date iniDate = timecard.getDate();
            Calendar newDate = Calendar.getInstance();
            Calendar newTime = Calendar.getInstance();

            newDate.setTime(iniDate);
            newTime.setTime(loginTime);
            newDate.set(newDate.get(Calendar.YEAR), newDate.get(Calendar.MONTH), newDate.get(Calendar.DATE),
                    newTime.get(Calendar.HOUR_OF_DAY), newTime.get(Calendar.MINUTE), newTime.get(Calendar.SECOND));

            timecard.setLoginTime(newDate.getTime());
        }

        if (logoutTime != null) {
            Date finDate = timecard.getDate();
            Calendar newDate = Calendar.getInstance();
            Calendar newTime = Calendar.getInstance();

            newDate.setTime(finDate);
            newTime.setTime(logoutTime);
            newDate.set(newDate.get(Calendar.YEAR), newDate.get(Calendar.MONTH), newDate.get(Calendar.DATE),
                    newTime.get(Calendar.HOUR_OF_DAY), newTime.get(Calendar.MINUTE), newTime.get(Calendar.SECOND));

            timecard.setLogoutTime(newDate.getTime());
        }


        if (timecard.getLogoutTime() != null) {
            if (timecard.getReason() == null) {
                addError("timecardForm.missingReason");
                return "fail";
            }
        }
        if (timecard.getReason() != null) {
            if (timecard.getLogoutTime() == null) {
                addError("timecardForm.missingLogout");
                return "fail";
            }
        }

        if (timecard.getUser() == null) {
            addError("timecardForm.missingUser");
            return "fail";
        }
        
        // Audit Fields
        if (operation.equals("add")) {
            timecard.setCreateOn(new Date());
            timecard.setCreatedBy(getCurrentUser());
        }
        timecard.setModifiedOn(new Date());
        timecard.setModifiedBy(getCurrentUser());
        
        addMessage("timecardForm.saved");
        timeTrackerService.saveTimecard(timecard);
        return "edit";
    }

    public String inactivate() {
        return "list";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Timecard getTimecard() {
        return timecard;
    }

    public void setTimecard(Timecard timecard) {
        this.timecard = timecard;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public Date getLogoutTime() {
        return logoutTime;
    }

    public void setLogoutTime(Date logoutTime) {
        this.logoutTime = logoutTime;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUsers() {
        users = userManager.getAll();
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }
}
